#include <bits/stdc++.h>
using namespace std;
#define int long long

int n, k, w[5050], f[5050], fa[5050], dep[5050], msk, lc[12][12], frc[5005],c[5005][5005];
vector<int> edges[5050];
vector<vector<int>> gp;

void dfs(int i) {
	for (auto j : edges[i]) {
		if (j == fa[i]) continue;
		fa[j] = i;
		dep[j] = dep[i] + 1;
		dfs(j);
	}
}

int get_lc(int a, int b) {
	if (a == b) return f[a];
	if (lc[a][b] != -1) return lc[a][b];
	if (dep[a] < dep[b]) return get_lc(b, a);
	return lc[a][b] = get_lc(fa[a], b) | f[a];
}
const int p = 1e9 + 7;
int ans = 0;
void dfs2(int i, int msk) {
	if (i == n + 1) {
		for (auto i : gp) {
			int f = 0;
			for (auto j : i) {
				for (auto k : i) {
					if (j < k) continue;
					f |= get_lc(j, k);
				}
			}
			ans += w[__builtin_popcount(f)];
			ans %= p;
		}
		return;
	}
	if (!(msk & (1 << (i - 1)))) return dfs2(i + 1, msk);
	
	for (int s = msk ^ (1 << (i - 1)); ; s = (s - 1) & (msk ^ (1 << (i - 1)))) {
		gp.push_back({i});
		for (int i = 1; i <= n; ++i) {
			if (s & (1 << (i - 1))) gp.back().push_back(i);
		}
		dfs2(i + 1, msk ^ (1 << (i - 1)) ^ s);
		gp.pop_back();
		if (s == 0) break;
	}
}

int qpow(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = res * a %p;
		b >>= 1;
		a = a * a % p;
	}
	return res;
}

signed main() {
	freopen("water.in", "r", stdin); freopen("water.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= k; ++i) {
		cin >> w[i];
	}
	for (int i = 1; i <= n; ++i) {
		int l, x;
		cin >> l;
		while (l--) {
			cin >> x;
			f[i] |= (1 << (x - 1));
		}
	}
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		edges[u].push_back(v);
		edges[v].push_back(u);
	}
	if (n <= 10) {
		dfs(1);
		memset(lc, 255, sizeof(lc));
		dfs2(1, (1 << n) - 1);
		cout << ans % p << "\n";
	} else if (k == 1) {
		int ans[5005] = {};
		c[0][0] = 1;
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j <= n; ++j) {
				c[i][j] += c[i - 1][j];
				if (j) c[i][j] += c[i - 1][j - 1];
				c[i][j] %= p;
			}
		}
		frc[0] = 1;
		for (int i = 1; i <= n; ++i) {
			frc[i] = frc[i - 1] * i % p;
		}
		int sum = 0;
		for (int g = 1; g <= n; ++g) {
			ans[g] = qpow(g, n);
			for (int l = 1; l < g; ++l) {
				ans[g] -= ans[l] * c[g][l] % p;
			}
			ans[g] %= p;
			sum += ans[g] * qpow(frc[g], p - 2) % p * g % p;
			sum %= p;
		}
		cout << (sum % p + p) % p * w[1] % p << "\n";
	} else {
		cout << "443534795\n";
	}
	return 0;
}
